import Vue from 'vue';
import Router from 'vue-router';
import HomePage from '@/components/HomePage';
import Category from '@/components/Category';
import ShoppingCart from '@/components/ShoppingCart';
import UserCenter from '@/components/UserCenter';
import TabIndex from '@/components/common/TabIndex';
import Login from '@/components/Login';
import Myhome from '@/components/MyHome';
Vue.use(Router);
const router = new Router({
  routes: [
    {
      path: '/login',
      name: '登录',
      component: Login
    },
    {
      path: '/myhome',
      name: '测滑首页',
      component: Myhome
    },

    // {
    //   path: "/user/:id",
    //   component: user
    // },
    {
      path: '/index',
      name: '首页',
      component: TabIndex,
      children: [
        {
          path: "homePage",
          name:"首页",
          component: HomePage,
          selected:"HomePage"
        },
        {
          path: "category",
          name:"分类",
          component: Category
        },
        {
          path: "shoppingCart",
          name:"购物车",
          meta:{
            requireAuth: true
          },
          component: ShoppingCart
        },
        {
          path: "userCenter",
          name:"个人中心",
          meta:{
            requireAuth: true
          },
          component: UserCenter
        }
      ]
    },
    {
      path: '/',
      redirect: '/index/homePage'
    }

  ],

});


router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {
    if (localStorage.getItem('userInfo')) {
      console.log(localStorage.getItem('userInfo'));
      next()
    } else {// 没登录则跳转到登录界面
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
});

export default router;
